package com.itheima.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.domain.Order;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface OrderDao extends BaseMapper<Order> {
    @Select("select * from orders where buyerId = #{buyerId} and status != -1")
    List<Order> findBuyerOrderListById(@Param("buyerId") int buyerId);

    @Select("select * from orders where orderId = #{orderId}")
    Order findOrderById(@Param("orderId") int orderId);

    @Select("select * from orders where orderNumber = #{orderNumber}")
    Order findOrderByOrderNumber(@Param("orderNumber") String orderNumber);

    //找出卖家卖了的
    @Select("SELECT * FROM orders WHERE sellerId = #{sellerId} AND status NOT IN (0, 3, 4, -1)")
    List<Order> findValidOrdersBySellerId(@Param("sellerId") int sellerId);

    //找出买家购买了的
    @Select("SELECT * FROM orders WHERE buyerId = #{buyerId} AND status NOT IN (0, 3, 4, -1)")
    List<Order> findValidOrdersByBuyerId(@Param("buyerId") int buyerId);

    @Insert("insert into orders (productId,buyerId,sellerId,orderNumber,price,status,createTime,address,hintInfo,buyerPhone,sellerPhone)" +
            "values(#{productId},#{buyerId},#{sellerId},#{orderNumber},#{price},#{status},#{createTime},#{address},#{hintInfo},#{buyerPhone},#{sellerPhone})")
    int insertOrder(Order order);

    @Update("update orders set status = -1 where orderId = #{orderId}")
    int updateStatus1i(@Param("orderId") int orderId);

    @Update("update orders set status = 1 , paymentTime = #{paymentTime}  where orderId = #{orderId}")
    int updateStatus1(@Param("paymentTime") Date paymentTime, @Param("orderId") int orderId);

    @Update("update orders set status = 2 where orderId = #{orderId}")
    int updateStatus2(@Param("orderId") int orderId);

    @Update("update orders set status = 3 where orderId = #{orderId}")
    int updateStatus3(@Param("orderId") int orderId);

    @Update("update orders set status = 4 where orderId = #{orderId}")
    int updateStatus4(@Param("orderId") int orderId);

    @Update("update orders set buyerPhone = #{buyerPhone}, address = #{address}, hintInfo = #{hintInfo} where orderId = #{orderId}")
    int editOrder(@Param("buyerPhone") String buyerPhone,
                  @Param("address") String address,
                  @Param("hintInfo") String hintInfo,
                  @Param("orderId") int orderId);
}
